<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>文件上传-ajax</title>
		<script src="js/axios.min.js"></script>
		<script src="js/jquery.min.js"></script>
		<script>
			function ajax_upload1(){
				debugger
				var url = "http://localhost:8084/myminio/fileupload2";
				//获取formData:
			    var file_inp1 = document.getElementById("file_inp1");
				var formData = new FormData();
				formData.append("pfile", file_inp1.files[0]); 
				var options = {headers:{ "Content-Type":"multipart/form-data"} };
				  axios.post(url,formData,options)
				  .then(function (response) {
						debugger
						console.log(response.data);
						let div_ret1 = document.getElementById("div_ret1");
						div_ret1.innerHTML = response.data.msg.toString();
					  })
				  .catch(function (error) {
					  console.log("catch error:");
					  console.log(error);
					  });
				
			}
			
			function ajax_upload2(){
				debugger
                $("#div_ret1").text("开始上传...");
				var url2 = "http://localhost:8084/myminio/fileupload2";
				//获取formData:
			    var file_inp1 = document.getElementById("file_inp1");
				var formData = new FormData();
				formData.append("pfile", file_inp1.files[0]); 
				//var options = {headers:{ "Content-Type":"multipart/form-data"} };
                $.ajax({
                    url: url2,
                    type: "POST",
                    data: formData,
                    /**
                    contentType: "multipart/form-data",
                    *必须false才会自动加上正确的Content-Type
                    */
                    contentType: false,
                    /**
                    * 必须false才会避开jQuery对 formdata 的默认处理
                    * XMLHttpRequest会对 formdata 进行正确的处理
                    */
                    processData: false,
                    success: function (data) {
						debugger
                        $("#div_ret1").text(data.toString());
                    },
                    error: function () {
                        //alert("上传失败！");
                        $("#div_ret1").text("error!");
                    }
                });
  				
			}
		</script>
	</head>
	<body>
		ajax文件上传功能<br>
    <form id="upload_form1" action="http://localhost:8084/myminio/fileupload1" method="post" enctype="multipart/form-data">
        <!--method必须为post enctype必须设置为multipart/form-data否则后台的HttpFileCollection的Count属性为0-->
        <input id="file_inp1" type="file" name="pfile" /><br>
		按pfile作为name<br>
        <br>
		<!-- <input id="file_submit1" type="submit"  value="上传文件" /> -->
    </form> <br>
	<div id="div_ret1" ></div><br>
	<input id="btn1" type="button" onclick="ajax_upload1()" value="上传文件/axios" /><br>
	<input id="btn2" type="button" onclick="ajax_upload2()" value="上传文件/jquery" /><br>
	
	
	</body>
</html>
